local extension = Package:new("ze_heg_ld")
extension.extensionName = "ze_heg"

local U = require "packages/utility/utility"
local H = require "packages/hegemony/util"

Fk:loadTranslationTable{
  ["ze_heg_ld"] = "君临天下",
  ["zhonger_ld_heg"] = "君临",
  ["zhonger_lord_heg"] = "君主",
}

local dengai = General(extension, "zhonger_ld_heg__dengai", "wei", 4)
dengai.mainMaxHpAdjustedValue = -1
dengai:addSkill("ld__tuntian")
dengai:addSkill("ld__jixi")
dengai:addSkill("ziliang")
Fk:loadTranslationTable{
  ["zhonger_ld_heg__dengai"] = "邓艾",
  ["#zhonger_ld_heg__dengai"] = "矫然的壮士",
  ["designer:zhonger_ld_heg__dengai"] = "官方",
  ["illustrator:zhonger_ld_heg__dengai"] = "Amo",
  ["~zhonger_ld_heg__dengai"] = "君不知臣，臣不知君。罢了……罢了！",
}

local caohong = General(extension, "zhonger_ld_heg__caohong", "wei", 4)
caohong:addSkill("heyi")
caohong:addRelatedSkill("ld__feiying")
local ldhuyuan_active = fk.CreateActiveSkill{
  name = "#ldhuyuan_active",
  mute = true,
  card_num = 1,
  target_num = 1,
  card_filter = function(self, to_select, selected, targets)
    return #selected == 0 and (Fk:getCardById(to_select).type == Card.TypeEquip or Fk:getCardById(to_select).type ~= Card.TypeEquip)
  end,
  target_filter = function(self, to_select, selected, cards)
    return #selected == 0 and #cards == 1
  end,
  can_use = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local card = Fk:getCardById(effect.cards[1])
    room:notifySkillInvoked(player, "ldhuyuan", "support")
    if card.type == Card.TypeEquip then
      room:moveCards({
        ids = effect.cards,
        from = player.id,
        to = target.id,
        toArea = Card.PlayerEquip,
        moveReason = fk.ReasonPut,
      })
      if not player.dead then
        local targets = table.map(table.filter(room.alive_players, function(p)
          return #p:getCardIds("ej") > 0 end), Util.IdMapper)
        local to2 = room:askForChoosePlayers(player, targets, 1, 1, "#ldhuyuan_discard-choose", self.name, false, true)
        local cid = room:askForCardChosen(player, room:getPlayerById(to2[1]), "ej", self.name)
        room:throwCard({cid}, self.name, room:getPlayerById(to2[1]), player)
      end
    elseif card.type ~= Card.TypeEquip then
      room:obtainCard(target.id, effect.cards, false, fk.ReasonGive, player.id)
    end
  end,
}
local ldhuyuan = fk.CreateTriggerSkill{
  name = "ldhuyuan",
  anim_type = "support",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Finish
  end,
  on_cost = function(self, event, target, player, data)
    player.room:askForUseActiveSkill(player, "#ldhuyuan_active", "#ldhuyuan-invoke", true)
  end,
}
ldhuyuan:addRelatedSkill(ldhuyuan_active)
caohong:addSkill(ldhuyuan)
caohong:addCompanions("zhonger_st_heg__caoren")
Fk:loadTranslationTable{
  ["zhonger_ld_heg__caohong"] = "曹洪",
  ["#zhonger_ld_heg__caohong"] = "魏之福将",
  ["designer:zhonger_ld_heg__caohong"] = "官方",
  ["illustrator:zhonger_ld_heg__caohong"] = "官方",
  ["ldhuyuan"] = "护援",
  [":ldhuyuan"] = "结束阶段，你可选择：1.将一张非装备牌交给一名角色；2.将一张装备牌置入一名角色的装备区，然后弃置场上的一张牌。",
  ["#ldhuyuan_active"] = "护援",
  ["#ldhuyuan-invoke"] = "护援：你可以将选择一张牌和一名角色",
  ["#ldhuyuan_discard-choose"] = "护援：弃置一名角色场上的一张牌",
  ["$ldhuyuan1"] = "将军，这件兵器可还趁手？",
  ["$ldhuyuan2"] = "宝马配英雄！哈哈哈哈……",
  ["cv:zhonger_ld_heg__caohong"] = "官方",
  ["~zhonger_ld_heg__caohong"] = "福兮祸所伏……",
}

local jiangwei = General(extension, "zhonger_ld_heg__jiangwei", "shu", 4)
jiangwei.deputyMaxHpAdjustedValue = -1
jiangwei:addSkill("tiaoxin")
jiangwei:addSkill("yizhi")
jiangwei:addSkill("tianfu")
jiangwei:addRelatedSkill("ld__guanxing")
jiangwei:addRelatedSkill("ld__kanpo")
Fk:loadTranslationTable{
  ["$tiaoxin_zhonger_ld_heg__jiangwei1"] = "小小娃娃，乳臭未干。",
  ["$tiaoxin_zhonger_ld_heg__jiangwei2"] = "快滚回去，叫你主将出来！",
  ["zhonger_ld_heg__jiangwei"] = "姜维",
  ["#zhonger_ld_heg__jiangwei"] = "龙的衣钵",
  ["designer:zhonger_ld_heg__jiangwei"] = "官方",
  ["~zhonger_ld_heg__jiangwei"] = "臣等正欲死战，陛下何故先降……",
}

local jiangfei = General(extension, "zhonger_ld_heg__jiangwanfeiyi", "shu", 3)
jiangfei:addSkill("ld__shengxi")
jiangfei:addSkill("shoucheng")
Fk:loadTranslationTable{
  ["zhonger_ld_heg__jiangwanfeiyi"] = "蒋琬费祎",
  ["#zhonger_ld_heg__jiangwanfeiyi"] = "社稷股肱",
  ["designer:zhonger_ld_heg__jiangwanfeiyi"] = "淬毒",
  ["illustrator:zhonger_ld_heg__jiangwanfeiyi"] = "cometrue",
  ["~zhonger_ld_heg__jiangwanfeiyi"] = "墨守成规，终为其害啊……",
}

local xusheng = General(extension, "zhonger_ld_heg__xusheng", "wu", 4)
xusheng:addSkill("yicheng")
xusheng:addCompanions("zhonger_st_heg__dingfeng")
Fk:loadTranslationTable{
  ["zhonger_ld_heg__xusheng"] = "徐盛",
  ["#zhonger_ld_heg__xusheng"] = "江东的铁壁",
  ["designer:zhonger_ld_heg__xusheng"] = "淬毒",
  ["illustrator:zhonger_ld_heg__xusheng"] = "天信",
  ["~zhonger_ld_heg__xusheng"] = "可怜一身胆略，尽随一抔黄土……",
}

local jiangqin = General(extension, "zhonger_ld_heg__jiangqin", "wu", 4)
jiangqin:addSkill("ld__shangyi")
jiangqin:addSkill("niaoxiang")
jiangqin:addCompanions("zhonger_st_heg__zhoutai")
Fk:loadTranslationTable{
  ["zhonger_ld_heg__jiangqin"] = "蒋钦",
  ["#zhonger_ld_heg__jiangqin"] = "祁奚之器",
  ["designer:zhonger_ld_heg__jiangqin"] = "淬毒",
  ["illustrator:zhonger_ld_heg__jiangqin"] = "天空之城",
  ["cv:zhonger_ld_heg__jiangqin"] = "小六",
  ["~zhonger_ld_heg__jiangqin"] = "竟破我阵法...",
}

local yuji = General(extension, "zhonger_ld_heg__yuji", "qun", 3)
yuji:addSkill("qianhuan")
Fk:loadTranslationTable{
  ["zhonger_ld_heg__yuji"] = "于吉",
  ["#zhonger_ld_heg__yuji"] = "魂绕左右",
  ["designer:zhonger_ld_heg__yuji"] = "淬毒",
  ["illustrator:zhonger_ld_heg__yuji"] = "G.G.G.",
  ["~zhonger_ld_heg__yuji"] = "幻化之物，终是算不得真呐。",
}

local hetaihou = General(extension, "zhonger_ld_heg__hetaihou", "qun", 3, 3, General.Female)
hetaihou:addSkill("zhendu")
hetaihou:addSkill("qiluan")
Fk:loadTranslationTable{
  ["zhonger_ld_heg__hetaihou"] = "何太后",
  ["#zhonger_ld_heg__hetaihou"] = "弄权之蛇蝎",
  ["cv:zhonger_ld_heg__hetaihou"] = "水原",
  ["illustrator:zhonger_ld_heg__hetaihou"] = "KayaK&木美人",
  ["designer:zhonger_ld_heg__hetaihou"] = "淬毒",
  ["~zhonger_ld_heg__hetaihou"] = "你们男人造的孽，非要说什么红颜祸水……",
}

local lidian = General(extension, "zhonger_ld_heg__lidian", "wei", 3)
lidian:addSkill("xunxun")
lidian:addSkill("wangxi")
lidian:addCompanions("zhonger_st_heg__yuejin")
Fk:loadTranslationTable{
  ["zhonger_ld_heg__lidian"] = "李典",
  ["#zhonger_ld_heg__lidian"] = "深明大义",
  ["designer:zhonger_ld_heg__lidian"] = "KayaK",
  ["illustrator:zhonger_ld_heg__lidian"] = "张帅",
  ["~zhonger_ld_heg__lidian"] = "报国杀敌，虽死犹荣……",
}

local zangba = General(extension, "zhonger_ld_heg__zangba", "wei", 4)
local hengjiang = fk.CreateTriggerSkill{
  name = "zhonger_ld_heg__hengjiang",
  anim_type = "masochism",
  events = { fk.Damaged },
  can_trigger = function(self, _, target, player, _)
    if target ~= player or not player:hasSkill(self) then return false end
    local current = player.room.current
    return current ~= nil and not current.dead
  end,
  on_use = function(_, _, _, player, data)
    local room = player.room
    local target = room.current
    if target ~= nil and not target.dead then
      room:doIndicate(player.id, {target.id})
      room:addPlayerMark(target, "@zhonger_ld_heg__hengjiang-turn", data.damage)
      room:addPlayerMark(target, MarkEnum.MinusMaxCardsInTurn, data.damage)
    end
  end
}
local hengjiangdelay = fk.CreateTriggerSkill{
  name = "#zhonger_ld_heg__hengjiang_delay",
  anim_type = "drawcard",
  events = { fk.TurnEnd },
  --FIXME:如何体现这个技能是延迟效果？
  can_trigger = function(_, _, target, player, _)
    if player.dead or player:usedSkillTimes(hengjiang.name) == 0 then return false end
    local room = player.room
    local discard_ids = {}
    room.logic:getEventsOfScope(GameEvent.Phase, 1, function (e)
      if e.data[2] == Player.Discard then
        table.insert(discard_ids, {e.id, e.end_id})
      end
      return false
    end, Player.HistoryTurn)
    if #discard_ids > 0 then
      if #room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function (e)
        local in_discard = false
        for _, ids in ipairs(discard_ids) do
          if #ids == 2 and e.id > ids[1] and e.id < ids[2] then
            in_discard = true
            break
          end
        end
        if in_discard then
          for _, move in ipairs(e.data) do
            if move.from == target.id and move.moveReason == fk.ReasonDiscard then
              for _, info in ipairs(move.moveInfo) do
                if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
                  return true
                end
              end
            end
          end
        end
        return false
      end, Player.HistoryTurn) > 0 then
        return false
      end
    end
    return true
  end,
  on_cost = Util.TrueFunc,
  on_use = function(_, _, _, player, _)
    local room = player.room
    local target = room.current
    player:drawCards(target:getMark("@zhonger_ld_heg__hengjiang-turn"), hengjiang.name)
  end,
}
hengjiang:addRelatedSkill(hengjiangdelay)
zangba:addSkill(hengjiang)
zangba:addCompanions("zhonger_st_heg__zhangliao")
Fk:loadTranslationTable{
  ['zhonger_ld_heg__zangba'] = '臧霸',
  ["#zhonger_ld_heg__zangba"] = "节度青徐",
  ["illustrator:zhonger_ld_heg__zangba"] = "HOOO",
  ["zhonger_ld_heg__hengjiang"] = '横江',
  [":zhonger_ld_heg__hengjiang"] = "当你受到1点伤害后，若当前回合角色手牌上限大于0，你可以令其本回合手牌上限-1。"..
    "然后若其本回合弃牌阶段内没有弃牌，你摸X张牌。(X为你此回合发动“横江”的次数)",
  ["@zhonger_ld_heg__hengjiang-turn"] = '横江',
  ["#zhonger_ld_heg__hengjiang_delay"] = '横江',
  ["$zhonger_ld_heg__hengjiang1"] = '霸必奋勇杀敌，一雪夷陵之耻！',
  ["$zhonger_ld_heg__hengjiang2"] = '江横索寒，阻敌绝境之中！',
  ['~zhonger_ld_heg__zangba'] = '断刃沉江，负主重托……',
}

local madai = General(extension, "zhonger_ld_heg__madai", "shu", 4)
madai:addSkill("heg_madai__mashu")
madai:addSkill("re__qianxi")
madai:addCompanions("zhonger_st_heg__machao")
Fk:loadTranslationTable{
  ["zhonger_ld_heg__madai"] = "马岱",
  ["#zhonger_ld_heg__madai"] = "临危受命",
  ["designer:zhonger_ld_heg__madai"] = "官方",
  ["illustrator:zhonger_ld_heg__madai"] = "Thinking",
  ["heg_madai__mashu"] = "马术",
  [":heg_madai__mashu"] = "锁定技，你与其他角色的距离-1。",
  ["~zhonger_ld_heg__madai"] = "我怎么会死在这里……",
}

local mifuren = General(extension, "zhonger_ld_heg__mifuren", "shu", 3, 3, General.Female)
mifuren:addSkill("guixiu")
mifuren:addSkill("cunsi")
mifuren:addRelatedSkill("yongjue")
Fk:loadTranslationTable{
  ['zhonger_ld_heg__mifuren'] = '糜夫人',
  ["#zhonger_ld_heg__mifuren"] = "乱世沉香",
  ["designer:zhonger_ld_heg__mifuren"] = "淬毒",
  ["illustrator:zhonger_ld_heg__mifuren"] = "木美人",
  ["~zhonger_ld_heg__mifuren"] = "阿斗被救，妾身再无牵挂…",
}

local sunce = General(extension, "zhonger_ld_heg__sunce", "wu", 4)
sunce.deputyMaxHpAdjustedValue = -1
sunce:addCompanions { "zhonger_st_heg__zhouyu", "zhonger_st_heg__taishici", "zhonger_st_heg__daqiao" }
sunce:addSkill("jiang")
sunce:addSkill("yingyang")
sunce:addSkill("hunshang")
sunce:addRelatedSkill("heg_sunce__yingzi")
sunce:addRelatedSkill("heg_sunce__yinghun")
Fk:loadTranslationTable{
  ['zhonger_ld_heg__sunce'] = '孙策',
  ["#zhonger_ld_heg__sunce"] = "江东的小霸王",
  ["designer:zhonger_ld_heg__sunce"] = "KayaK（韩旭）",
  ["illustrator:zhonger_ld_heg__sunce"] = "木美人",
  ["~zhonger_ld_heg__sunce"] = "内事不决问张昭，外事不决问周瑜……",
}

local chengdong = General(extension, "zhonger_ld_heg__chenwudongxi", "wu", 4)
chengdong:addSkill("ld__duanxie")
chengdong:addSkill("ld__fenming")
Fk:loadTranslationTable{
  ['zhonger_ld_heg__chenwudongxi'] = '陈武董袭',
  ["#zhonger_ld_heg__chenwudongxi"] = "壮怀激烈",
  ["designer:zhonger_ld_heg__chenwudongxi"] = "淬毒",
  ["illustrator:zhonger_ld_heg__chenwudongxi"] = "地狱许",
  ["~zhonger_ld_heg__chenwudongxi"] = "杀身卫主，死而无憾！",
}

local dongzhuo = General(extension, "zhonger_ld_heg__dongzhuo", "qun", 4)
dongzhuo:addSkill("hengzheng")
dongzhuo:addSkill("baoling")
dongzhuo:addRelatedSkill("benghuai")
Fk:loadTranslationTable{
  ['zhonger_ld_heg__dongzhuo'] = '董卓',
  ["#zhonger_ld_heg__dongzhuo"] = "魔王",
  ["designer:zhonger_ld_heg__dongzhuo"] = "KayaK（韩旭）",
  ["illustrator:zhonger_ld_heg__dongzhuo"] = "巴萨小马",
  ['~zhonger_ld_heg__dongzhuo'] = '为何人人……皆与我为敌？',
}

local zhangren = General(extension, "zhonger_ld_heg__zhangren", "qun", 4)
zhangren:addSkill("chuanxin")
zhangren:addSkill("fengshi")
Fk:loadTranslationTable{
  ['zhonger_ld_heg__zhangren'] = '张任',
  ["#zhonger_ld_heg__zhangren"] = "索命神射",
  ["designer:zhonger_ld_heg__zhangren"] = "淬毒",
  ["illustrator:zhonger_ld_heg__zhangren"] = "DH",
  ['~zhonger_ld_heg__zhangren'] = '本将军败于诸葛，无憾……',
}

local xunyou = General(extension, "zhonger_ld_heg__xunyou", "wei", 3)
xunyou:addSkill("ld__qice")
xunyou:addSkill("zhiyu")
xunyou:addCompanions("zhonger_st_heg__xunyu")
Fk:loadTranslationTable{
  ["zhonger_ld_heg__xunyou"] = "荀攸",
  ["#zhonger_ld_heg__xunyou"] = "曹魏的谋主",
  ["designer:zhonger_ld_heg__xunyou"] = "淬毒",
  ["illustrator:zhonger_ld_heg__xunyou"] = "心中一凛",
  ["~zhonger_ld_heg__xunyou"] = "主公，臣下……先行告退……",
}

local bianfuren = General(extension, "zhonger_ld_heg__bianfuren", "wei", 3, 3, General.Female)
local yuejian = fk.CreateTriggerSkill{
  name = "zhonger_ld_heg__yuejian",
  anim_type = "support",
  frequency = Skill.Compulsory,
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return H.compareKingdomWith(player, target) and player:hasSkill(self) and target.phase == Player.Discard 
      and target:getMark("zhonger_ld_heg__yuejian-turn") == 0 and player.room.current == target
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:doIndicate(player.id, {target.id})
    room:addPlayerMark(target, "_yuejian_maxcard-turn", 1)
  end,

  refresh_events = {fk.TargetSpecified},
  can_refresh = function(self, event, target, player, data)
    return H.compareKingdomWith(target, player) and target == player.room.current
      and target:getMark("zhonger_ld_heg__yuejian-turn") == 0 and data.firstTarget and data.card.type ~= Card.TypeEquip
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, id in ipairs(AimGroup:getAllTargets(data.tos)) do
      if not H.compareKingdomWith(room:getPlayerById(id), target) then
        room:addPlayerMark(target, "zhonger_ld_heg__yuejian-turn", 1)
        break
      end
    end
  end,
}
local yuejian_maxcards = fk.CreateMaxCardsSkill{
  name = "#yuejian_maxcards",
  fixed_func = function(self, player)
    if player:getMark("_yuejian_maxcard-turn") > 0 then
      return player.maxHp
    end
  end
}
yuejian:addRelatedSkill(yuejian_maxcards)
bianfuren:addCompanions("zhonger_st_heg__caocao")
bianfuren:addSkill("ld__wanwei")
bianfuren:addSkill(yuejian)

Fk:loadTranslationTable{
  ["zhonger_ld_heg__bianfuren"] = "卞夫人",
  ["#zhonger_ld_heg__bianfuren"] = "奕世之雍容",
  ["illustrator:zhonger_ld_heg__bianfuren"] = "雪君S",
  ["zhonger_ld_heg__yuejian"] = "约俭",
  [":zhonger_ld_heg__yuejian"] = "锁定技，与你势力相同角色的弃牌阶段开始时，若其本回合处于与你势力相同状态时未对其他势力角色使用过牌，其本回合的手牌上限改为其体力上限。",
  ["$zhonger_ld_heg__yuejian1"] = "无文绣珠玉，器皆黑漆。",
  ["$zhonger_ld_heg__yuejian2"] = "性情约俭，不尚华丽。",
  ["$ld__wanwei_zhonger_ld_heg__bianfuren1"] = "梁、沛之间，非子廉无有今日。",
  ["$ld__wanwei_zhonger_ld_heg__bianfuren2"] = "正使祸至，共死何苦！",
  ["~zhonger_ld_heg__bianfuren"] = "心肝涂地，惊愕断绝。",
}

local shamoke = General(extension, "zhonger_ld_heg__shamoke", "shu", 4)
shamoke:addSkill("jilis")
Fk:loadTranslationTable{
  ['zhonger_ld_heg__shamoke'] = '沙摩柯',
  ["#zhonger_ld_heg__shamoke"] = "五溪蛮王",
  ["illustrator:zhonger_ld_heg__shamoke"] = "LiuHeng",
  ["designer:zhonger_ld_heg__shamoke"] = "韩旭",
  ['~zhonger_ld_heg__shamoke'] = '五溪蛮夷，不可能输！',
}

local masu = General(extension, "zhonger_ld_heg__masu", "shu", 3)
masu:addSkill("sanyao")
masu:addSkill("ld__zhiman")
Fk:loadTranslationTable{
  ['zhonger_ld_heg__masu'] = '马谡',
  ["#zhonger_ld_heg__masu"] = "帷幄经谋",
  ["designer:zhonger_ld_heg__masu"] = "点点",
  ["illustrator:zhonger_ld_heg__masu"] = "蚂蚁君",
  ["~zhonger_ld_heg__masu"] = "败军之罪，万死难赎……" ,
}

local lingtong = General(extension, "zhonger_ld_heg__lingtong", "wu", 4)
lingtong:addSkill("xuanlve")
lingtong:addSkill("yongjin")
lingtong:addCompanions("zhonger_st_heg__ganning")
Fk:loadTranslationTable{
  ['zhonger_ld_heg__lingtong'] = '凌统',
  ["#zhonger_ld_heg__lingtong"] = "豪情烈胆",
  ["designer:zhonger_ld_heg__lingtong"] = "韩旭",
  ["illustrator:zhonger_ld_heg__lingtong"] = "F.源",
  ["~zhonger_ld_heg__lingtong"] = "大丈夫，不惧死亡……",
}

local lvfan = General(extension, "zhonger_ld_heg__lvfan", "wu", 3)
local hgdiaodu = fk.CreateTriggerSkill{
  name = "hgdiaodu",
  anim_type = "drawcard",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return false end
    if event == fk.CardUsing then return H.compareKingdomWith(target, player) and data.card.type == Card.TypeEquip and (player:hasShownSkill(self) or player == target)
    else return target == player and target.phase == Player.Play and table.find(player.room.alive_players, function(p)
    return H.compareKingdomWith(p, player) and #p:getCardIds(Player.Equip) > 0 end) end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardUsing then
      return room:askForSkillInvoke(target, self.name, nil, "#hgdiaodu-invoke")
    else
      local targets = table.map(table.filter(room.alive_players, function(p)
        return H.compareKingdomWith(p, player) and #p:getCardIds(Player.Equip) > 0 end), Util.IdMapper)
      if #targets == 0 then return false end
      local target = room:askForChoosePlayers(player, targets, 1, 1, "#hgdiaodu-choose", self.name, true)
      if #target > 0 then
        self.cost_data = target[1]
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.CardUsing then
      return false
    else
      local room = player.room
      local target = room:getPlayerById(self.cost_data)
      local cid = room:askForCardChosen(player, target, "e", self.name)
      room:obtainCard(player, cid, true, fk.ReasonPrey)
      if not table.contains(player:getCardIds(Player.Hand), cid) then return false end
      local card = Fk:getCardById(cid)
      if player.dead then return false end
      local targets = table.map(table.filter(room.alive_players, function(p) return p ~= player and p ~= target end), Util.IdMapper)
      local to = room:askForChoosePlayers(player, targets, 0, 1, "#hgdiaodu-give:::" .. card:toLogString(), self.name, target ~= player)
      if #to > 0 then
        room:moveCardTo(card, Card.PlayerHand, room:getPlayerById(to[1]), fk.ReasonGive, self.name, nil, true, player.id)
      end
    end
  end,
}

local diaodu = fk.CreateTriggerSkill{
  name = "#spdiaodu",
  anim_type = "drawcard",
  frequency = Skill.Compulsory,
  events = {fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return false end
    if event == fk.CardUsing then return H.compareKingdomWith(target, player) and data.card.type == Card.TypeEquip and (player:hasShownSkill(self) or player == target)
    else return target == player and target.phase == Player.Play and table.find(player.room.alive_players, function(p)
    return H.compareKingdomWith(p, player) and #p:getCardIds(Player.Equip) > 0 end) end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardUsing and player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 then
      return room:askForSkillInvoke(target, self.name, nil, "#hgdiaodu-invoke")
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.CardUsing then
      target:drawCards(1, self.name)
    end
  end,
}

hgdiaodu:addRelatedSkill(diaodu)
lvfan:addSkill(hgdiaodu)
lvfan:addSkill("diancai")
Fk:loadTranslationTable{
  ['zhonger_ld_heg__lvfan'] = '吕范',
  ["#zhonger_ld_heg__lvfan"] = "忠笃亮直",
  ["designer:zhonger_ld_heg__lvfan"] = "韩旭",
  ["illustrator:zhonger_ld_heg__lvfan"] = "铭zmy",
  ['hgdiaodu'] = '调度',
  ['#spdiaodu'] = '调度',
  [':hgdiaodu'] = '①当与你势力相同的角色每回合首次使用装备牌时，其摸一张牌。②出牌阶段开始时，你可获得与你势力相同的一名角色装备区里的一张牌，然后你可将此牌交给另一名角色。',
  ["#hgdiaodu-invoke"] = "调度：你可摸一张牌",
  ["#hgdiaodu-choose"] = "调度：你可获得与你势力相同的一名角色装备区里的一张牌",
  ["#hgdiaodu-give"] = "调度：将%arg交给另一名角色",
  ["$hgdiaodu1"] = "开源节流，作法于凉。",
  ["$hgdiaodu2"] = "调度征求，省行薄敛。",
  ["$diancai_zhonger_ld_heg__lvfan1"] = "量入为出，利析秋毫。",
  ["$diancai_zhonger_ld_heg__lvfan2"] = "天下熙攘，皆为利往。",
  ["~zhonger_ld_heg__lvfan"] = "印绶未下，疾病已发。",
}

local zuoci = General(extension, "zhonger_ld_heg__zuoci", "qun", 3)
local wudile = fk.CreateTriggerSkill{
  name = "#wudile",
  anim_type = "defensive",
  frequency = Skill.Compulsory,
  events = {fk.PreCardEffect},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and player.id == data.to and player:getMark("#@@草泥马") ~= 0
  end,
  on_use = Util.TrueFunc,
  after_use = function (room,player)
    room:setPlayerMark(player, "#@@草泥马", 0)
  end
  
}
local huashen_blacklist = {}
local function Gethuashen(player, n)
  local room = player.room
  local generals = table.filter(room.general_pile, function (name)
    return not table.contains(huashen_blacklist, name)
  end)
  local mark = U.getPrivateMark(player, "&hunpo")
  for _ = 1, n do
    if #generals == 0 then break end
    local g = table.remove(generals, math.random(#generals))
    table.insert(mark, g)
    table.removeOne(room.general_pile, g)
  end
  U.setPrivateMark(player, "&hunpo", mark)
end
local gzjh = fk.CreateTriggerSkill{
  name = "gzjh",
  anim_type = "masochism",
  events = {fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player and player:hasSkill("gzjh", true)
  end,
  on_use = function(self, event, target, player, data)
    Gethuashen(player, 1)
  end,
}
local gzyg = fk.CreateViewAsSkill{
  name = "gzyg",
  pattern = ".",
  interaction = function()
    local names = {}
    local mark = Self:getMark("@$gzyg-turn")
    for _, id in ipairs(Fk:getAllCardIds()) do
      local card = Fk:getCardById(id)
      if (card.type == Card.TypeBasic or (card.type == Card.TypeTrick and card.sub_type ~= Card.SubtypeDelayedTrick)) and not card.is_derived then
        local to_use = Fk:cloneCard(card.name)
        if ((Fk.currentResponsePattern == nil and Self:canUse(to_use) and not Self:prohibitUse(to_use)) or
        (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(to_use))) then
          if mark == 0 or (not table.contains(mark, card.trueName)) then
            table.insertIfNeed(names, card.name)
          end
        end
      end
    end
    if #names == 0 then return false end
    return UI.ComboBox { choices = names }
  end,
  view_as = function(self, cards)
    if #cards ~= 0 or not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcards(cards)
    card.skillName = self.name
    return card
  end,
  before_use = function(self, player, use)
    local room = player.room
    local marke = player:getMark("@$gzyg-turn")
    if marke == 0 then marke = {} end
    table.insert(marke, use.card.trueName)
    local mark = U.getPrivateMark(player, "&hunpo")
    if #mark == 0 then return end
    local name = room:askForGeneral(player, mark, 1, true)
    local general = Fk.generals[name]
    local kingdom = tostring(general.kingdom)
    local mar = tostring(general.subkingdom)
      for _, p in ipairs(room:getAlivePlayers(player)) do
        if p.kingdom ~= kingdom and p.kingdom ~= mar and p.kingdom ~= "wild" then
          room:setPlayerMark(p, "#@@草泥马", 1)
          room:handleAddLoseSkills(p, "#wudile")
        end
      end
    table.removeOne(mark, name)
    U.setPrivateMark(player, "&hunpo", mark)
  end,
  enabled_at_play = function(self, player)
    return #U.getPrivateMark(player, "&hunpo") > 0 and not player:isNude()
  end,
  enabled_at_response = function(self, player, response)
    return false
  end,
  after_use = function(self, player, use)
    local room = player.room
      for _, p in ipairs(room:getAlivePlayers(player)) do
        if p:hasSkill(wudile, true, true) then
          room:handleAddLoseSkills(p, '-#wudile')
          room:setPlayerMark(p, "#@@草泥马", 0)
        end
      end
      room:setPlayerMark(player, "#@@草泥马", 0)
      if player:hasSkill(wudile, true, true) then
        room:handleAddLoseSkills(player, '-#wudile') end
  end,
}

local xinsheng2 = fk.CreateTriggerSkill{
  name = "#fuckuu",
  events = {fk.GeneralRevealed},
  can_trigger = function(self, event, target, player, data)
    if target ~= player or not player:hasSkill(self) then return false end
    if event == fk.GeneralRevealed then
      if player:usedSkillTimes(self.name, Player.HistoryGame) == 0 then
        return not player:isFakeSkill(self)
      end
    else
      return false
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.GeneralRevealed then return true end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.GeneralRevealed then
      Gethuashen(player, 2)
    end
  end,
}
gzyg:addRelatedSkill(wudile)
gzyg:addRelatedSkill(xinsheng2)
zuoci:addSkill(gzjh)
zuoci:addSkill(gzyg)
zuoci:addCompanions("zhonger_ld_heg__yuji")
Fk:loadTranslationTable{
  ["zhonger_ld_heg__zuoci"] = "左慈",
  ["#zhonger_ld_heg__zuoci"] = "鬼影神道",
  ["illustrator:zhonger_ld_heg__zuoci"] = "吕阳",
  ["@[private]&hunpo"] = "魂",
  ["gzjh_re"] = "进行一次“汲魂”",
  ["#gzyg_trigger"] = "役鬼",
  ["gzyg"] = "役鬼",
  ["#wudile"] = "役鬼",
  ["$#wudile1"] = "容貌发肤，不过浮尘。",
  ["$#wudile2"] = "皮囊万千，吾皆可化。",
  [":gzyg"] = "当你首次明置此武将后，你将剩余武将牌堆中的两张武将牌扣置于武将牌上，称为“魂”。"..
    "出牌阶段，你可以移去一张“魂”，视为使用一张基本牌或普通锦囊牌使用。此虚拟牌结算过程中，所有牌仅对野心家以及“魂”势力的角色生效。",
  ["#js_lose_xinsheng"] = "请移除一张“魂”",
  ["gzjh"] = "汲魂",
  ["@$gzyg-turn"] = "役鬼",
  [":gzjh"] = "当你受到伤害后，你可以随机获得一张未加入游戏的武将牌作为“魂”。",
  ["#fuckuu"] = "役鬼",
  [":#fuckuu"] = "当你首次明置此武将后，你将剩余武将牌堆中的四张武将牌扣置于武将牌上，称为“魂”。",
  ["$gzyg1"] = "世间万物，贫道皆可化为其形。",
  ["$gzyg2"] = "尘身土塑，唯魂魄难得。",
  ["$gzjh1"] = "大成若缺，损亦无妨。",
  ["$gzjh2"] = "大盈若冲，新神自现。",
  ["~zhonger_ld_heg__zuoci"] = "仙人之逝，魂归九天。",
}

local lijueguosi = General(extension, "zhonger_ld_heg__lijueguosi", "qun", 4)
lijueguosi:addCompanions("zhonger_st_heg__jiaxu")
lijueguosi:addSkill("xiongsuan")
Fk:loadTranslationTable{
  ['zhonger_ld_heg__lijueguosi'] = '李傕郭汜',
  ["#zhonger_ld_heg__lijueguosi"] = "犯祚倾祸",
  ["designer:zhonger_ld_heg__lijueguosi"] = "千幻",
  ["illustrator:zhonger_ld_heg__lijueguosi"] = "旭",
  ["~zhonger_ld_heg__lijueguosi"] = "异心相争，兵败战损……",
}

local cuiyanmaojie = General(extension, "zhonger_ld_heg__cuiyanmaojie", "wei", 3)
cuiyanmaojie:addSkill("ld__zhengbi")
cuiyanmaojie:addSkill("ld__fengying")
cuiyanmaojie:addCompanions("zhonger_st_heg__caopi")
Fk:loadTranslationTable{
  ["zhonger_ld_heg__cuiyanmaojie"] = "崔琰毛玠",
  ["#zhonger_ld_heg__cuiyanmaojie"] = "日出月盛",
  ["designer:zhonger_ld_heg__cuiyanmaojie"] = "Virgopaladin（韩旭）",
  ["illustrator:zhonger_ld_heg__cuiyanmaojie"] = "兴游",
  ["~zhonger_ld_heg__cuiyanmaojie"] = "为世所痛惜，冤哉……",
}

local yujin = General(extension, "zhonger_ld_heg__yujin", "wei", 4)
yujin:addSkill("ld__jieyue")
yujin:addCompanions("zhonger_st_heg__xiahoudun")
Fk:loadTranslationTable{
  ['zhonger_ld_heg__yujin'] = '于禁',
  ["#zhonger_ld_heg__yujin"] = "讨暴坚垒",
  ["designer:zhonger_ld_heg__yujin"] = "官方",
  ["illustrator:zhonger_ld_heg__yujin"] = "biou09",
  ["$ld__jieyue_zhonger_ld_heg__yujin1"] = "按丞相之命，此部今由余统摄。",
  ["$ld__jieyue_zhonger_ld_heg__yujin2"] = "奉法行令，事上之节，岂有宽宥之力。",
  ["~zhonger_ld_heg__yujin"] = "如今临危处难，却负丞相三十年之赏识，哎。",
}

local wangping = General(extension, "zhonger_ld_heg__wangping", "shu", 4)
wangping:addCompanions("zhonger_ld_heg__jiangwanfeiyi")
local jianglue = fk.CreateActiveSkill{
  name = "zhonger_ld_heg__jianglue",
  frequency = Skill.Limited,
  anim_type = "support",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
  card_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local index = H.startCommand(player, self.name)
    local kingdom = H.getKingdom(player)
    for _, p in ipairs(room:getAlivePlayers()) do
    end
    local targets = table.map(table.filter(room.alive_players, function(p) return H.compareKingdomWith(p, player) and p ~= player end), Util.IdMapper)
    local tos = {}
    if #targets > 0 then
      room:doIndicate(player.id, targets)
      room:sortPlayersByAction(targets)
      for _, pid in ipairs(targets) do
        local p = room:getPlayerById(pid)
        if player.dead then break end
        if not p.dead and H.doCommand(p, self.name, index, player) then
          table.insert(tos, pid)
        end
      end
    end
    table.insert(tos, 1, player.id)
    local num = 0
    for _, pid in ipairs(tos) do
      local p = room:getPlayerById(pid)
      if not p.dead then
        room:changeMaxHp(p, 1)
        if not p.dead then
          if room:recover({
            who = p,
            num = 1,
            recoverBy = player,
            skillName = self.name
          }) then
            num = num + 1
          end
        end
      end
    end
    if num > 0 then player:drawCards(num, self.name) end
  end
}
wangping:addSkill(jianglue)
Fk:loadTranslationTable{
  ["zhonger_ld_heg__wangping"] = "王平",
  ["#zhonger_ld_heg__wangping"] = "键闭剑门",
  ["illustrator:zhonger_ld_heg__wangping"] = "zoo",
  ["zhonger_ld_heg__jianglue"] = "将略",
  [":zhonger_ld_heg__jianglue"] = "限定技，出牌阶段，你可选择一个“军令”，然后你对所有与你势力相同的角色发起此“军令”。你加1点体力上限，回复1点体力，所有执行“军令”的角色各加1点体力上限，回复1点体力。然后你摸X张牌（X为以此法回复体力的角色数）。",
  ["$zhonger_ld_heg__jianglue1"] = "奇谋为短，将略为要。",
  ["$zhonger_ld_heg__jianglue2"] = "为将者，需有谋略。",
  ["~zhonger_ld_heg__wangping"] = "无当飞军，也有困于深林之时……",
}

local fazheng = General(extension, "zhonger_ld_heg__fazheng", "shu", 3)
fazheng:addCompanions("zhonger_st_heg__liubei")
fazheng:addSkill("ld__enyuan")
local wushengXH = fk.CreateViewAsSkill{
  name = "fa__wusheng",
  anim_type = "offensive",
  pattern = "slash",
  card_filter = function(self, to_select, selected)
    if #selected == 1 then return false end
    return (H.getHegLord(Fk:currentRoom(), Self) and H.getHegLord(Fk:currentRoom(), Self):hasSkill("shouyue")) or Fk:getCardById(to_select).color == Card.Red
  end,
  view_as = function(self, cards)
    if #cards ~= 1 then
      return nil
    end
    local c = Fk:cloneCard("slash")
    c.skillName = self.name
    c:addSubcard(cards[1])
    return c
  end,
}
local wusheng_targetmod = fk.CreateTargetModSkill{
  name = "#fa__wusheng_targetmod",
  anim_type = "offensive",
  bypass_distances = function (self, player, skill, card, to)
    return player:hasSkill(wushengXH) and skill.trueName == "slash_skill" and card.suit == Card.Diamond
  end
}
wushengXH:addRelatedSkill(wusheng_targetmod)
local paoxiaoTriggerXH = fk.CreateTriggerSkill{
  name = "#fa__paoxiaoTrigger",
  events = {fk.CardUsing},
  anim_type = "offensive",
  visible = false,
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if target ~= player or not player:hasSkill(self) or data.card.trueName ~= "slash" then return false end
    local events = player.room.logic:getEventsOfScope(GameEvent.UseCard, 2, function(e) 
      local use = e.data[1]
      return use.from == player.id and use.card.trueName == "slash" 
    end, Player.HistoryTurn)
    return #events == 2 and events[2].id == player.room.logic:getCurrentEvent().id
  end,
  on_use = function(self, event, target, player, data)
    player:drawCards(1, self.name)
  end,

  refresh_events = {fk.CardUsing, fk.TargetSpecified, fk.CardUseFinished},
  can_refresh = function(self, event, target, player, data)
    if player ~= target then return false end -- 摆一下
    if event == fk.CardUsing then
      return player:hasSkill(self) and data.card.trueName == "slash" and player:usedCardTimes("slash") > 1
    else
      local room = player.room
      if not H.getHegLord(room, player) or not H.getHegLord(room, player):hasSkill("shouyue") then return false end
      if event == fk.CardUseFinished then
        return (data.extra_data or {}).xhHsPaoxiaoNullifiled
      else
        return data.card.trueName == "slash" and player:hasSkill("fa__paoxiao") and room:getPlayerById(data.to):isAlive()
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardUsing then
      player:broadcastSkillInvoke("hs__paoxiao")
      room:doAnimate("InvokeSkill", {
        name = "paoxiao",
        player = player.id,
        skill_type = "offensive",
      })
    elseif event == fk.CardUseFinished then
      for key, num in pairs(data.extra_data.xhHsPaoxiaoNullifiled) do
        local p = room:getPlayerById(tonumber(key))
        if p:getMark(fk.MarkArmorNullified) > 0 then
          room:removePlayerMark(p, fk.MarkArmorNullified, num)
        end
      end
      data.xhHsPaoxiaoNullifiled = nil
    else
      room:addPlayerMark(room:getPlayerById(data.to), fk.MarkArmorNullified)
      data.extra_data = data.extra_data or {}
      data.extra_data.xhHsPaoxiaoNullifiled = data.extra_data.xhHsPaoxiaoNullifiled or {}
      data.extra_data.xhHsPaoxiaoNullifiled[tostring(data.to)] = (data.extra_data.xhHsPaoxiaoNullifiled[tostring(data.to)] or 0) + 1
    end
  end,
}
local paoxiaoXH = fk.CreateTargetModSkill{
  name = "fa__paoxiao",
  frequency = Skill.Compulsory,
  bypass_times = function(self, player, skill, scope)
    if player:hasSkill(self) and skill.trueName == "slash_skill"
      and scope == Player.HistoryPhase then
      return true
    end
  end,
}
paoxiaoXH:addRelatedSkill(paoxiaoTriggerXH)
local longdanXH = fk.CreateViewAsSkill{
  name = "fa__longdan",
  pattern = "slash,jink",
  card_filter = function(self, to_select, selected)
    if #selected == 1 then return false end
    local _c = Fk:getCardById(to_select)
    local c
    if _c.trueName == "slash" then
      c = Fk:cloneCard("jink")
    elseif _c.name == "jink" then
      c = Fk:cloneCard("slash")
    else
      return false
    end
    return (Fk.currentResponsePattern == nil and Self:canUse(c)) or (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(c))
  end,
  view_as = function(self, cards)
    if #cards ~= 1 then
      return nil
    end
    local _c = Fk:getCardById(cards[1])
    local c
    if _c.trueName == "slash" then
      c = Fk:cloneCard("jink")
    elseif _c.name == "jink" then
      c = Fk:cloneCard("slash")
    end
    c.skillName = self.name
    c:addSubcard(cards[1])
    return c
  end,
}
local longdanAfterXH = fk.CreateTriggerSkill{
  name = "#xuanhuo__longdan_after",
  anim_type = "offensive",
  visible = false,
  events = {fk.CardEffectCancelledOut, fk.CardUsing, fk.CardResponding},
  can_trigger = function(self, event, target, player, data)
    if event == fk.CardEffectCancelledOut then
      if data.card.trueName ~= "slash" then return false end
      if target == player then -- 龙胆杀
        return table.contains(data.card.skillNames, "fa__longdan")
      elseif data.to == player.id then -- 龙胆闪
        for _, card in ipairs(data.cardsResponded) do
          if card.name == "jink" and table.contains(card.skillNames, "fa__longdan") then
            return true
          end
        end
      end
    else
      local room = player.room
      return player == target and H.getHegLord(room, player) and table.contains(data.card.skillNames, "fa__longdan") and H.getHegLord(room, player):hasSkill("shouyue")
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardEffectCancelledOut then
      if target == player then
        local targets = table.map(room:getOtherPlayers(room:getPlayerById(data.to)), Util.IdMapper)
        if #targets == 0 then return false end
        local target = room:askForChoosePlayers(player, targets, 1, 1, "#longdan_slash-ask::" .. data.to, self.name, true)
        if #target > 0 then
          self.cost_data = target[1]
          return true
        end
        return false
      else
        local targets = table.map(table.filter(room:getOtherPlayers(target), function(p) return
          p ~= player and p:isWounded()
        end), Util.IdMapper)
        if #targets == 0 then return false end
        local target = room:askForChoosePlayers(player, targets, 1, 1, "#longdan_jink-ask::" .. target.id , self.name, true)
        if #target > 0 then
          self.cost_data = target[1]
          return true
        end
      end
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data)
    if event == fk.CardEffectCancelledOut then
      if target == player then
        room:damage{
          from = player,
          to = to,
          damage = 1,
          skillName = self.name,
        }
      else
        room:recover({
          who = to,
          num = 1,
          recoverBy = player,
          skillName = self.name,
        })
      end
    else
      player:drawCards(1, self.name)
    end
  end,
}
longdanXH:addRelatedSkill(longdanAfterXH)
local tieqiXH = fk.CreateTriggerSkill{
  name = "fa__tieqi",
  anim_type = "offensive",
  events = {fk.TargetSpecified},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.card.trueName == "slash"
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(data.to)
    local judge = {
      who = player,
      reason = self.name,
      pattern = ".|.|spade,club,heart,diamond",
    }
    if player.dead then return end
    local choices = {}
    if to.general ~= "anjiang" then
      table.insert(choices, to.general)
    end
    if to.deputyGeneral ~= "anjiang" then
      table.insert(choices, to.deputyGeneral)
    end
    if #choices > 0 then
      local choice
      if H.getHegLord(room, player) and #choices > 1 and H.getHegLord(room, player):hasSkill("shouyue") then
        choice = choices
      else
        choice = {room:askForChoice(player, choices, self.name, "#hs__tieqi-ask::" .. to.id)}
      end
      local record = type(to:getMark("@hs__tieqi-turn")) == "table" and to:getMark("@hs__tieqi-turn") or {}
      for _, c in ipairs(choice) do
        table.insertIfNeed(record, c)
        room:setPlayerMark(to, "@hs__tieqi-turn", record)
        local mark = type(to:getMark("_hs__tieqi-turn")) == "table" and to:getMark("_hs__tieqi-turn") or {}
        for _, skill_name in ipairs(Fk.generals[c]:getSkillNameList()) do
          if Fk.skills[skill_name].frequency ~= Skill.Compulsory then
            table.insertIfNeed(mark, skill_name)
          end
        end
        room:setPlayerMark(to, "_hs__tieqi-turn", mark)
      end
    end
    room:judge(judge)
    if judge.card.suit ~= nil then
      local suits = {}
      table.insert(suits, judge.card:getSuitString())
      if #room:askForDiscard(to, 1, 1, false, self.name, true, ".|.|" .. table.concat(suits, ","), "#hs__tieqi-discard:::" .. judge.card:getSuitString()) == 0 then
        data.disresponsive = true
      end
    end
  end,
}
local tieqiInvalidityXH = fk.CreateInvaliditySkill {
  name = "#fa__tieqi_invalidity",
  invalidity_func = function(self, from, skill)
    if from:getMark("_hs__tieqi-turn") ~= 0 then
      return table.contains(from:getMark("_hs__tieqi-turn"), skill.name) and
      (skill.frequency ~= Skill.Compulsory and skill.frequency ~= Skill.Wake) and not skill.name:endsWith("&")
    end
  end
}
tieqiXH:addRelatedSkill(tieqiInvalidityXH)
local liegongXH = fk.CreateTriggerSkill{
  name = "fa__liegong",
  anim_type = "offensive",
  events = {fk.TargetSpecified},
  can_trigger = function(self, event, target, player, data)
    if not (target == player and player:hasSkill(self)) then return end
    local room = player.room
    local to = room:getPlayerById(data.to)
    local num = #to:getCardIds(Player.Hand)
    local filter = num <= player:getAttackRange() or num >= player.hp
    return data.card.trueName == "slash" and filter and player.phase == Player.Play
  end,
  on_use = function(self, event, target, player, data)
    data.disresponsiveList = data.disresponsiveList or {}
    table.insert(data.disresponsiveList, data.to)
  end,
}
local liegongXHAR = fk.CreateAttackRangeSkill{
  name = "#fa__liegongAR",
  correct_func = function(self, from, to)
    if from:hasSkill("fa__liegong") then
      for _, p in ipairs(Fk:currentRoom().alive_players) do
        if string.find(p.general, "lord") and p:hasSkill("shouyue") and p.kingdom == from.kingdom then
          return 1
        end
      end
    end
    return 0
  end,
}
local liegong_t = fk.CreateTargetModSkill{
  name = "#fa__liegong_targetmod",
    bypass_distances =  function(self, player, skill, card, target)
    if skill.trueName == "slash_skill" and player:hasSkill(liegong) then
      return card and target and player:distanceTo(target) <= card.number
    end
  end,
}
liegongXH:addRelatedSkill(liegongXHAR)
liegongXH:addRelatedSkill(liegong_t)
local kuangguXH = fk.CreateTriggerSkill{
  name = "fa__kuanggu",
  anim_type = "drawcard",
  events = {fk.Damage},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player and (data.extra_data or {}).kuanggucheck
  end,
  on_trigger = function(self, event, target, player, data)
    self.cancel_cost = false
    for i = 1, data.damage do
      self:doCost(event, target, player, data)
      if self.cost_data == "Cancel" then break end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local choices = {"draw1", "Cancel"}
    if player:isWounded() then
      table.insert(choices, 2, "recover")
    end
    self.cost_data = room:askForChoice(player, choices, self.name)
    return self.cost_data ~= "Cancel"
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if self.cost_data == "recover" then
      room:recover({
        who = player,
        num = 1,
        recoverBy = player,
        skillName = self.name
      })
    elseif self.cost_data == "draw1" then
      player:drawCards(1, self.name)
    end
  end,

  refresh_events = {fk.BeforeHpChanged},
  can_refresh = function(self, event, target, player, data)
    return data.damageEvent and player == data.damageEvent.from and player:distanceTo(target) < 2 and player:distanceTo(target) > -1
  end,
  on_refresh = function(self, event, target, player, data)
    data.damageEvent.extra_data = data.damageEvent.extra_data or {}
    data.damageEvent.extra_data.kuanggucheck = true
  end,
}
local xuanhuo = fk.CreateTriggerSkill{
  name = "ze__xuanhuo",
  mute = true,
  refresh_events = {fk.EventAcquireSkill, fk.EventLoseSkill, fk.Deathed, fk.GeneralRevealed, fk.GeneralHidden},
  can_refresh = function(self, event, target, player, data)
    if player ~= target then return false end
    if event == fk.Deathed then return player:hasSkill(self.name, true, true)
    elseif event == fk.EventAcquireSkill or event == fk.EventLoseSkill then return data == self
    else return true end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local players = room.alive_players
    local fazhengs = table.filter(players, function(p) return p:hasShownSkill(self) end)
    local xuanhuo_map = {}
    for _, p in ipairs(players) do
      local will_attach = false
      for _, fazheng in ipairs(fazhengs) do
        if (fazheng ~= p and H.compareKingdomWith(fazheng, p)) then
          will_attach = true
          break
        end
      end
      xuanhuo_map[p] = will_attach
    end
    for p, v in pairs(xuanhuo_map) do
      if v ~= p:hasSkill("ze__xuanhuo_other&") then
        room:handleAddLoseSkills(p, v and "ze__xuanhuo_other&" or "-ze__xuanhuo_other&", nil, false, true)
      end
    end
  end,
}
local xuanhuoOther = fk.CreateActiveSkill{
  name = "ze__xuanhuo_other&",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and table.find(Fk:currentRoom().alive_players, function(p)
      return p:hasSkill(xuanhuo) and H.compareKingdomWith(p, player) and p ~= Self
    end)
  end,
  card_num = 1,
  card_filter = function(self, to_select, selected)
    return #selected < 1 and Fk:currentRoom():getCardArea(to_select) == Player.Hand
  end,
  target_num = 0,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local targets = table.filter(room:getOtherPlayers(player), function(p) return p:hasShownSkill(xuanhuo) and H.compareKingdomWith(p, player) end)
    if #targets == 0 then return false end
    local to
    if #targets == 1 then
      to = targets[1]
    else
      to = room:getPlayerById(room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, nil, self.name, false)[1])
    end
    room:doIndicate(player.id, {to.id})
    to:broadcastSkillInvoke(self.name)
    room:moveCardTo(effect.cards, Player.Hand, to, fk.ReasonGive, self.name, nil, false, to.id)
    if player:isNude() or not room:askForDiscard(player, 1, 1, true, self.name, false, nil, "#ze__xuanhuo-ask") then return false end
    if player.dead then return false end
    local all_choices = {"fa__wusheng", "fa__paoxiao", "fa__longdan","fa__tieqi", "fa__liegong", "fa__kuanggu"}
    local choices = {}
    local skills = {}
    for _, p in ipairs(room.alive_players) do
      for _, s in ipairs(p.player_skills) do
        table.insert(skills, s.name)
      end
    end
    for _, skill in ipairs(all_choices) do
      local skillNames = {skill, skill:sub(10)}
      local can_choose = true
      for _, sname in ipairs(skills) do
        if table.contains(skillNames, sname) then
          can_choose = false
          break
        end
      end
      if can_choose then table.insert(choices, skill) end
    end
    if #choices == 0 then return false end

    local choice = room:askForChoice(player, choices, self.name, "#ze__xuanhuo-choice", true, all_choices)
    room:handleAddLoseSkills(player, choice, nil)
    local record = type(player:getMark("@ze__xuanhuo_skills-turn")) == "table" and player:getMark("@ze__xuanhuo_skills-turn") or {}
    table.insert(record, choice)
    room:setPlayerMark(player, "@ze__xuanhuo_skills-turn", record)
  end,
}
local xuanhuoOtherLose = fk.CreateTriggerSkill{
  name = "#ze__xuanhuo_other_lose&",
  visible = false,
  refresh_events = {fk.EventPhaseStart, fk.GeneralRevealed},
  can_refresh = function(self, event, target, player, data)
    if type(player:getMark("@ze__xuanhuo_skills-turn")) ~= "table" then return false end
    if event == fk.EventPhaseStart then
      return target == player and player.phase == Player.NotActive
    else
      return true
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      local _skills = player:getMark("@ze__xuanhuo_skills-turn")
      local skills = "-" .. table.concat(_skills, "|-")
      room:handleAddLoseSkills(player, skills, nil)
      room:setPlayerMark(player, "@ze__xuanhuo_skills-turn", 0)
    else
      local skills = {}
      table.forEach(room.alive_players, function(p) table.insertTable(skills, p.player_skills) end)
      local xuanhuoSkills = player:getMark("@ze__xuanhuo_skills-turn")
      if type(xuanhuoSkills) == "table" then
        local detachList = {}
        for _, skill in ipairs(skills) do
          local skillName = "xuanhuo__" .. skill.name
          if (table.contains(xuanhuoSkills, skillName)) then
            table.removeOne(xuanhuoSkills, skillName)
            table.insert("-" .. skillName)
          end
        end
        if #detachList > 0 then
          room:handleAddLoseSkills(player, table.concat(detachList, "|"), nil)
          room:setPlayerMark(player, "@ze__xuanhuo_skills-turn", #xuanhuoSkills > 0 and xuanhuoSkills or 0)
        end
      end
    end
  end,
}
xuanhuoOther:addRelatedSkill(xuanhuoOtherLose)
Fk:addSkill(xuanhuoOther)
fazheng:addSkill(xuanhuo)
fazheng:addRelatedSkill(wushengXH)
fazheng:addRelatedSkill(paoxiaoXH)
fazheng:addRelatedSkill(longdanXH)
fazheng:addRelatedSkill(tieqiXH)
fazheng:addRelatedSkill(liegongXH)
fazheng:addRelatedSkill(kuangguXH)
Fk:loadTranslationTable{
  ["zhonger_ld_heg__fazheng"] = "法正",
  ["#zhonger_ld_heg__fazheng"] = "蜀汉的辅翼",
  ["illustrator:zhonger_ld_heg__fazheng"] = "黑白画谱",
  ["ze__xuanhuo"] = "眩惑",
  [":ze__xuanhuo"] = "与你势力相同的其他角色的出牌阶段限一次，其可交给你一张手牌，然后其弃置一张牌，选择下列技能中的一个：〖武圣〗〖咆哮〗〖龙胆〗〖铁骑〗〖烈弓〗〖狂骨〗（场上已有的技能无法选择）。其于此回合内或明置有其以此法选择的技能的武将牌之前拥有其以此法选择的技能。",
  ["ze__xuanhuo_other&"] = "眩惑",
  [":ze__xuanhuo_other&"] = "你可交给法正一张手牌，然后弃置一张牌，选择下列技能中的一个：〖武圣〗〖咆哮〗〖龙胆〗〖铁骑〗〖烈弓〗〖狂骨〗（场上已有的技能无法选择）。你于此回合内或明置有以此法选择的技能的武将牌之前拥有以此法选择的技能。",
  ["fa__wusheng"] = "武圣",
  ["fa__paoxiao"] = "咆哮",
  ["fa__longdan"] = "龙胆",
  ["fa__tieqi"] = "铁骑",
  ["fa__liegong"] = "烈弓",
  ["fa__kuanggu"] = "狂骨",
  [":fa__wusheng"] = "你可将一张红色牌当【杀】使用或打出。你使用的方块【杀】无距离限制。",
  [":fa__paoxiao"] = "锁定技，你使用【杀】无次数限制。当你于一个回合内使用第二张【杀】时，你摸一张牌。",
  [":fa__longdan"] = "你可将【闪】当【杀】使用或打出，当此【杀】被一名角色使用的【闪】抵消后，你可对另一名角色造成1点伤害。你可将【杀】当【闪】使用或打出，当一名角色使用的【杀】被此【闪】抵消后，你可令另一名其他角色回复1点体力。",
  [":fa__tieqi"] = "当你使用【杀】指定目标后，你可判定，令其本回合一张明置的武将牌非锁定技失效，其需弃置一张与判定结果花色相同的牌，否则其不能使用【闪】抵消此【杀】。",
  [":fa__liegong"] = "你对至其距离不大于此【杀】点数的角色使用【杀】无距离关系的限制。当你于出牌阶段内使用【杀】指定目标后，若其手牌数不小于你的体力值或不大于你的攻击范围，你可令其不能使用【闪】响应此【杀】。",
  [":fa__kuanggu"] = "当你对距离1以内的角色造成1点伤害后，你可摸一张牌或回复1点体力。",
  ["#fa__paoxiaoTrigger"] = "咆哮",
  ["#xuanhuo__longdan_after"] = "龙胆",
  ["#ze__xuanhuo-ask"] = "眩惑：弃置一张牌",
  ["@ze__xuanhuo_skills-turn"] = "眩惑",
  ["$ze__xuanhuo1"] = "收人钱财，替人消灾。",
  ["$ze__xuanhuo2"] = "哼，叫你十倍奉还！",
  ["~zhonger_ld_heg__fazheng"] = "汉室复兴，我，是看不到了……",
}

local lukang = General(extension, "zhonger_ld_heg__lukang", "wu", 3, 3, General.Male)
lukang:addSkill("ld__keshou")
lukang:addSkill("ld__zhuwei")
lukang:addCompanions("zhonger_st_heg__luxun")
Fk:loadTranslationTable{
  ["zhonger_ld_heg__lukang"] = "陆抗",
  ["#zhonger_ld_heg__lukang"] = "孤柱扶厦",
  ["illustrator:zhonger_ld_heg__lukang"] = "王立雄",
  ["~zhonger_ld_heg__lukang"] = "抗仅以君子之交待叔子，未有半分背国之念啊……",
}

local wuguotai = General(extension, "zhonger_ld_heg__wuguotai", "wu", 3, 3, General.Female)
wuguotai:addSkill("ld__buyi")
wuguotai:addSkill("ganlu")
wuguotai:addCompanions("zhonger_st_heg__sunjian")

Fk:loadTranslationTable{
  ['zhonger_ld_heg__wuguotai'] = '吴国太',
  ["#zhonger_ld_heg__wuguotai"] = "武烈皇后",
  ["illustrator:zhonger_ld_heg__wuguotai"] = "李秀森",
  ["$ganlu_zhonger_ld_heg__wuguotai1"] = "玄德实乃佳婿呀。",
  ["$ganlu_zhonger_ld_heg__wuguotai2"] = "好一个郎才女貌，真是天作之合啊。",
  ["~zhonger_ld_heg__wuguotai"] = "诸位卿家，还请尽力辅佐仲谋啊……",
}

local yuanshu = General(extension, "zhonger_ld_heg__yuanshu", "qun", 4)
yuanshu:addCompanions("zhonger_st_heg__jiling")
local yongsi = fk.CreateTriggerSkill{
  name = "zhonger_ld_heg__yongsi",
  anim_type = "drawcard",
  frequency = Skill.Compulsory,
  events = {fk.EventPhaseStart, fk.DrawNCards, fk.TargetConfirmed},
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if target ~= player or not player:hasSkill(self) then return false end
    if event == fk.TargetConfirmed then
      return data.card.trueName == "known_both" and not player:isKongcheng()
    else
      if not H.isBigKingdomPlayer(player) or table.find(player.room.alive_players, function(p)
        return table.find(p:getEquipments(Card.SubtypeTreasure), function(cid)
          return Fk:getCardById(cid).name == "jade_seal"
        end)
      end) then return false end
      return event == fk.DrawNCards or player.phase == Player.Play
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      local card = Fk:cloneCard("known_both")
      local max_num = card.skill:getMaxTargetNum(player, card)
      local targets = {}
      for _, p in ipairs(room:getOtherPlayers(player)) do
        if not player:isProhibited(p, card) then
          table.insert(targets, p.id)
        end
      end
      if #targets == 0 or max_num == 0 then return end
      local to = room:askForChoosePlayers(player, targets, 1, max_num, "#zhonger_ld_heg__yongsi__jade_seal-ask", self.name, false)
      if #to > 0 then
        self.cost_data = to
        return true
      end
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(self.name)
    if event == fk.EventPhaseStart then
      room:notifySkillInvoked(player, self.name, "control")
      local targets = table.map(self.cost_data, Util.Id2PlayerMapper)
      room:useVirtualCard("known_both", nil, player, targets, self.name)
    elseif event == fk.DrawNCards then
      room:notifySkillInvoked(player, self.name, "drawcard")
      data.n = data.n + 1
    else
      room:notifySkillInvoked(player, self.name, "negative")
      if not player:isKongcheng() then
        player:showCards(player:getCardIds(Player.Hand))
      end
    end
  end,
}
local yongsiBig = H.CreateBigKingdomSkill{
  name = "#zhonger_ld_heg__yongsi_big",
  fixed_func = function(self, player)
    return player:hasShownSkill(yongsi) and player.kingdom ~= "unknown" and not table.find(Fk:currentRoom().alive_players, function(p)
      return table.find(p:getEquipments(Card.SubtypeTreasure), function(cid)
        return Fk:getCardById(cid).name == "jade_seal"
      end)
    end)
  end
}
yongsi:addRelatedSkill(yongsiBig)
yuanshu:addSkill(yongsi)
yuanshu:addSkill("ld__weidi")

Fk:loadTranslationTable{
  ['zhonger_ld_heg__yuanshu'] = '袁术',
  ["#zhonger_ld_heg__yuanshu"] = "仲家帝",
  ["illustrator:zhonger_ld_heg__yuanshu"] = "YanBai",
  ['zhonger_ld_heg__yongsi'] = "庸肆",
  [':zhonger_ld_heg__yongsi'] = "锁定技，①若所有角色的装备区里均没有【玉玺】，你视为装备着【玉玺】；②当你成为【知己知彼】的目标后，展示所有手牌。",
  ["#zhonger_ld_heg__yongsi__jade_seal-ask"] = "庸肆：你视为使用一张【知己知彼】",
  ["$zhonger_ld_heg__yongsi1"] = "天下，即将尽归吾袁公路！",
  ["$zhonger_ld_heg__yongsi2"] = "朕今日雄踞淮南，明日便可一匡天下。",
  ["$ld__weidi_zhonger_ld_heg__yuanshu1"] = "传国玉玺在手，朕语便是天言！",
  ["$ld__weidi_zhonger_ld_heg__yuanshu2"] = "传朕旨意，诸部遵旨即可。",
  ["~zhonger_ld_heg__yuanshu"] = "仲朝国祚，本应千秋万代，薪传不息……",
}

local zhangxiu = General(extension, "zhonger_ld_heg__zhangxiu", "qun", 4)
zhangxiu:addSkill("ld__fudi")
zhangxiu:addSkill("ld__congjian")
zhangxiu:addCompanions("zhonger_st_heg__jiaxu")
Fk:loadTranslationTable{
  ['zhonger_ld_heg__zhangxiu'] = '张绣',
  ["#zhonger_ld_heg__zhangxiu"] = "北地枪王",
  ["designer:zhonger_ld_heg__zhangxiu"] = "官方",
  ["illustrator:zhonger_ld_heg__zhangxiu"] = "青岛磐蒲",
  ['~zhonger_ld_heg__zhangxiu'] = '若失文和，吾将何归？',
}

return extension
